package com.jingdianjichi.subject.service.impl;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.enums.IsDeleteEnum;
import com.jingdianjichi.subject.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {
    @Resource
    private SubjectCategoryService subjectCategoryService;



    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if(log.isInfoEnabled()) {
            log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectCategoryBO));
        }

        SubjectCategory subjectCategory = SubjectCategoryConverter.Instatnce.convertBOToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeleteEnum.UN_DELETED.getCode());
        subjectCategoryService.insert(subjectCategory);

    }

    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.Instatnce.convertBOToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeleteEnum.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategories= subjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBO> subjectCategoryBOS = SubjectCategoryConverter.Instatnce.convertCategoryToBO(subjectCategories);
        return subjectCategoryBOS;
    }

    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.Instatnce.convertBOToCategory(subjectCategoryBO);
        return subjectCategoryService.update(subjectCategory) > 0;
    }

    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.Instatnce.convertBOToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeleteEnum.DELETED.getCode());
        return subjectCategoryService.update(subjectCategory) > 0;
    }
}
